package com.example.chenjun.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.chenjun.myapplication.R;
import com.example.chenjun.myapplication.adapter.CookListAdapter;
import com.example.chenjun.myapplication.bean.CookBean;
import com.example.chenjun.myapplication.exeption.CookException;
import com.example.chenjun.myapplication.mvp.MvpActivity;
import com.example.chenjun.myapplication.mvp.contract.CookListContract;
import com.example.chenjun.myapplication.mvp.contract.LabelDetailContract;
import com.example.chenjun.myapplication.mvp.model.CookModel;
import com.example.chenjun.myapplication.mvp.presenter.CookListPresenter;
import com.example.chenjun.myapplication.mvp.presenter.LabelDetailPresenter;
import com.example.chenjun.myapplication.util.Logger;

import java.util.ArrayList;
import java.util.List;

public class CookListActivity extends MvpActivity<CookListPresenter, CookModel> implements CookListContract.CookListView {
    private int mCurIndex;
    private CookListAdapter mAdapter;
    private List<CookBean> mTotalBeans;
    private boolean mIsLoading;
    private boolean mFinishLoading;
    private boolean mHasMore = true;
    private String mLabelId;
    private String mSearchKey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTotalBeans = new ArrayList<>();
        setContentView(R.layout.app_activity_cook_list);
        ListView lv = findViewById(R.id.lv);
        mAdapter = new CookListAdapter();
        lv.setAdapter(mAdapter);
        Intent intent = getIntent();
        mLabelId = intent.getStringExtra("labelId");
        mSearchKey = intent.getStringExtra("searchKey");
        loadCookList();
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount - 1){
                    if (mHasMore && !mIsLoading) {
                        loadMore();
                    }
                }
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(CookListActivity.this, CookDetailActivity.class).putExtra("bean", mTotalBeans.get(position)));
            }
        });
    }

    private void loadCookList() {
        if (mLabelId != null){
            mPresenter.getLabelCookList(mLabelId, mCurIndex, 20);
        }else {
            mPresenter.searchCookList(mSearchKey, mCurIndex, 20);
        }
        mIsLoading = true;
    }

    private void complete(){
        mIsLoading = false;
        mCurIndex++;
    }

    private void finishLoadMore(){
        mHasMore = false;
    }

    private void loadMoreError(){
        mIsLoading = false;
    }

    private void loadMore(){
        loadCookList();
    }

    @Override
    public void getCookListSuccess(List<CookBean> list) {
        Logger.i(list.toString());
        mTotalBeans.addAll(list);
        mAdapter.setList(mTotalBeans);
        complete();
        if (list.size() != 20){
            finishLoadMore();
        }
    }

    @Override
    public void getCookListFailed(Exception e) {
        if (e instanceof CookException){
            finishLoadMore();
        }else {
            loadMoreError();
        }
        if (mTotalBeans == null){
            Toast.makeText(this, getString(R.string.no_such_cook), Toast.LENGTH_SHORT).show();
        }
    }
}
